package com.example.a117.wangyimusic;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ScrollingView;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by 117 on 2018/5/18.
 */

public class videoFragment extends Fragment {


    List<RadioButton> buttons = new ArrayList<RadioButton>();
    ImageViewAnimationHelper helper;
    float[] width;
    float margin = 15;
    RadioGroup collectionRadioGroup;
    ImageView instructionsIv;
    Handler handle;
    float sumWidth;
    ViewPager viewPager;
    FragmentTransaction transaction;
    FragmentManager fragmentManager;
    List<Fragment> fragmentList;
    FragmentPagerAdapter pagerAdapter;
    View view;
    @SuppressLint("HandlerLeak")
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle
                             savedInstanceState){

        view = inflater.inflate(R.layout.fragment_video,container,false);
        //HorizontalScrollView scrollView = view.findViewById(R.id.video_scroll);
        instructionsIv  = view.findViewById(R.id.instructionsIv);
        RadioButton recommendBt = view.findViewById(R.id.recommend_bt);
        RadioButton musicBt = view.findViewById(R.id.music_bt);
        RadioButton ShowtimeBt = view.findViewById(R.id.Showtime_bt);
        RadioButton MVBt = view.findViewById(R.id.MV_bt);
        RadioButton twoDimentionBt = view.findViewById(R.id.twoDimension_bt);
        RadioButton danceBt = view.findViewById(R.id.dance_bt);
        RadioButton gameBt = view.findViewById(R.id.game_bt);
        buttons.add(recommendBt);
        buttons.add(musicBt);
        buttons.add(ShowtimeBt);
        buttons.add(MVBt);
        buttons.add(twoDimentionBt);
        buttons.add(danceBt);
        buttons.add(gameBt);
        width = new float[7];

        collectionRadioGroup = view.findViewById(R.id.collectionRadioGroup);
        new Thread(){
            @Override
            public void run() {
                synchronized (this) {
                    try {
                        while (width[7]==0){
                            wait();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 0 ; i < 7 ; i++){
                    width[i] = buttons.get(i).getWidth();
                    sumWidth = collectionRadioGroup.getWidth();
                    Log.d(TAG, "width["+i+"] ="+width[i]);
                }
                Message message = new Message();
                message.what = 1;
                handle.sendMessage(message);
            }
        }.start();

        handle = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1){
                    helper = new ImageViewAnimationHelper(getContext(),instructionsIv,7,width,sumWidth);
                    collectionRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                            switch (i) {
                                case R.id.recommend_bt:
                                    helper.startAnimation(0);
                                    break;
                                case R.id.music_bt:
                                    helper.startAnimation(1);
                                    break;
                                case R.id.Showtime_bt:
                                    helper.startAnimation(2);
                                    break;
                                case R.id.MV_bt:
                                    helper.startAnimation(3);
                                    break;
                                case R.id.twoDimension_bt:
                                    helper.startAnimation(4);
                                    break;
                                case R.id.dance_bt:
                                    helper.startAnimation(5);
                                    break;
                                case R.id.game_bt:
                                    helper.startAnimation(6);
                                    break;
                                default:
                                    break;
                            }
                        }
                    });

                }
            }
        };

        fragmentManager = getFragmentManager()
        viewPager = view.findViewById(R.id.video_viewpager);
        pagerAdapter = new MyPagerAdapter(fragmentManager,getFragments());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                reFreshFragment(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return view;
    }

    public List<Fragment> getFragments() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new video_recommendFragment());
        fragmentList.add(new video_musicFragment());
        fragmentList.add(new video_ShowtimeFragment());
        fragmentList.add(new video_MVFragment());
        fragmentList.add(new video_twoDFragment());
        fragmentList.add(new video_danceFragment());
        fragmentList.add(new video_gameFragment());
        return fragmentList;
    }

    public void reFreshFragment(int position){
        //View view;
        //nitMenu();
        ImageView img;
        transaction = fragmentManager.beginTransaction();
        switch(position){
            case 0:{
                transaction.replace(R.id.video_fragment,new video_recommendFragment());
                transaction.commit();
                break;
            }
            case 1:{
                transaction.replace(R.id.main_fragment,new video_musicFragment());
                transaction.commit();
                break;
            }
            case 2:{
                transaction.replace(R.id.video_fragment,new video_ShowtimeFragment());
                transaction.commit();
                break;
            }
            case 3:{
                transaction.replace(R.id.main_fragment,new video_MVFragment());
                transaction.commit();
                break;
            }case 4:{
                transaction.replace(R.id.video_fragment,new video_twoDFragment());
                transaction.commit();
                break;
            }
            case 5:{
                transaction.replace(R.id.main_fragment,new video_danceFragment());
                transaction.commit();
                break;
            }
            case 6:{
                transaction.replace(R.id.main_fragment,new video_gameFragment());
                transaction.commit();
                break;
            }
            default:
                break;
        }
    }
}
